/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package database.dao;

import database.common.GenericDao;
import database.entity.Distributors;
import java.util.Date;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import java.util.List;

/**
 *
 * @author Administrator
 */
public class DistributorsDao extends GenericDao<Distributors, Integer> {

    public List<Distributors> search(String name, String address, String type) {
        Session session = sessionFactory.getCurrentSession();
        session.beginTransaction();
        Criteria c = session.createCriteria(Distributors.class);
        c.add(Restrictions.eq("status", 0));
        if (!name.isEmpty()) {
            c.add(Restrictions.like("name", "%" + name + "%"));
        }
        if (!address.isEmpty()) {
            c.add(Restrictions.like("address", "%" + address + "%"));
        }
        if (!type.equals("All")) {
            c.add(Restrictions.like("type", type));
        }
        List<Distributors> list = c.list();
        session.flush();
        session.getTransaction().commit();
        return list;
    }
    
    public List<Distributors> filter(String name, String address, String type, String phone) {
        Session session = sessionFactory.getCurrentSession();
        session.beginTransaction();
        Criteria c = session.createCriteria(Distributors.class);
        c.add(Restrictions.eq("status", 0));
        if (!name.isEmpty()) {
            c.add(Restrictions.like("name", "%" + name + "%"));
        }
        if (!address.isEmpty()) {
            c.add(Restrictions.like("address", "%" + address + "%"));
        }
        if (!type.equals("All")) {
            c.add(Restrictions.like("type", type));
        }
        if (!phone.isEmpty()) {
            c.add(Restrictions.like("phone", "%" + phone + "%"));
        }
        List<Distributors> list = c.list();
        session.flush();
        session.getTransaction().commit();
        return list;
    }
    
    public List<Distributors> getAllDistributor() {
        Session session = sessionFactory.getCurrentSession();
        session.beginTransaction();
        Criteria c = session.createCriteria(Distributors.class);
        c.add(Restrictions.eq("status", 0));
        List<Distributors> list = c.list();
        session.flush();
        session.getTransaction().commit();
        return list;
    }
}
